/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.gerenciadorfinanceiro.ejb.session;

import java.util.List;
import org.gerenciadorfinanceiro.ejb.entity.OutcomeSource;
import org.gerenciadorfinanceiro.ejb.session.interfaces.OutcomeSourceSessionLocal;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.gerenciadorfinanceiro.ejb.session.interfaces.OutcomeSourceSessionRemote;

/**
 *
 * @author eduardo
 */
@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class OutcomeSourceSessionBean implements OutcomeSourceSessionLocal, OutcomeSourceSessionRemote {

    @PersistenceContext
    private  EntityManager entityManager;
    
    public List<OutcomeSource> getAllSources() {
        Query q = entityManager.createNamedQuery("OutcomeSource.findAll");
        return q.getResultList();
    }

    public OutcomeSource getSourceByName(String name) {
        Query q = entityManager.createNamedQuery("OutcomeSource.findByDescriptionSource");
        q.setParameter("descriptionSource", name);

        try {
            return (OutcomeSource)q.getSingleResult();
        } catch (NoResultException exc) {
            return null;
        }

    }

    public OutcomeSource getSourceById(int id) {
        return entityManager.find(OutcomeSource.class, new Integer(id));
    }

    public void addOutcomeSource(OutcomeSource source) throws BusinessViolation {
        if (this.getSourceByName(source.getDescriptionSource()) != null) {
            throw new BusinessViolation("Fonte de despesa já existente.");
        }

        entityManager.merge(source);
    }

    public void updateOutcomeSource(OutcomeSource source) throws BusinessViolation {
        if (this.getSourceById(source.getId().intValue()) == null) {
            throw new BusinessViolation("Fonte de despesa inexistente.");
        }

        entityManager.merge(source);
    }

    public void deleteOutcomeSource(OutcomeSource source) throws BusinessViolation {
        if (this.getSourceByName(source.getDescriptionSource()) == null) {
            throw new BusinessViolation("Fonte de despesa inexistente.");
        }

        OutcomeSource sAux = entityManager.getReference(OutcomeSource.class, source.getId());
        entityManager.remove(sAux);
    }

    public void validaOutcomeSource(OutcomeSource os) throws BusinessViolation {
        boolean valid = false;
        String msg = "";

        if (os.getDescriptionSource() == null || os.getDescriptionSource().isEmpty()) {
            valid = false;
            msg = "Descricao invalida.";
        }

        if (os.getExpirationDay() != 0 && (os.getExpirationDay() < 1 || os.getExpirationDay() > 31)) {
            valid = false;
            msg = "Dia de fechamento mensal invalido.";
        }

        if (! valid) {
            throw new BusinessViolation(msg);
        }
    }
 
}
